package model.container;

/**
 * RegistrationContainer.java
 * Purpose: A container to hold registration objects
 * 
 * @author Hans Brems
 * @version 1.0 03/11/2011
 */

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

import model.Registration;
import model.Student;
import model.generator.RegistrationGenerator;

public class RegistrationContainer {

	private static RegistrationContainer registrationContainer;
	private Set<Registration> registrations;

	/* Make sure the class cannot be instantiated */
	private RegistrationContainer() {
		this.registrations = new TreeSet<Registration>();
	}

	public void generateData() {
		try {
			registrations = RegistrationGenerator.generateRegistrations(100);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/** Constructs a new course container object if none exists */
	public static RegistrationContainer getInstance() {
		if (registrationContainer == null) {
			registrationContainer = new RegistrationContainer();
		}
		return registrationContainer;
	}

	/** Returns the list of registrations */
	public Set<Registration> getRegistrations() {
		return (Set<Registration>)registrations;
	}

	/** Sets the list of registrations */
	public void setRegistrations(Set<Registration> registrations) {
		this.registrations = registrations;
	}

	/** ?????????????? */
	public void finishInitialisation() {
		RegistrationGenerator.finishRegistrationInitialisation();
	}

	/** Returns the number of registrations in the container */
	public int getRegistrationCount() {
		return registrations.size();
	}

	/** Clear the container */
	public void clear() {
		registrations.clear();
	}

	/** Adds a registration to the list */
	public void addRegistration(Registration registration) {
		registrations.add(registration);
	}

	/** Remove a registration to the list */
	public void removeCourse(Registration registration) {
		registrations.remove(registration);
	}

	/** Prints all registrations in the collection */
	public String printAllRegistrations() {
		String s = new String();
		for (Registration registration : registrations) {
			s += registration.toString() + "\n";
		}
		return s;
	}

	/** Searches for a course with a specified ID */
	public Registration getRegistrationByID(Object id) {
		for (Registration registration : this.registrations) {
			if (registration.getPrimaryKey() == id) {
				return registration;
			}
		}
		return null;
	}

	/** ?????????? */
	public Set<Registration> getRegistrationsForStudent(Student student) {
		Set<Registration> studentRegistrations = new HashSet<Registration>();
		if (student != null) {
			for (Registration registration : registrations) {
				if (student.equals(registration.getStudent())) {
					studentRegistrations.add(registration);
				}
			}
		}
		return studentRegistrations;
	}
	
	public Collection<Registration> getRegistrationsByStudent(Student student) {
		Collection<Registration> result = new ArrayList<Registration>();
		if (student != null) {
			for (Registration registration : this.registrations) {
				if (student.equals(registration.getStudent())) {
					result.add(registration);
				}
			}
		}
		return result;
	}

	public Set<Registration> getRegistrationByOfferedCourse(Object offeredcourseKey) {
		Set<Registration> offeredCourseRegistrations = new HashSet<Registration>();
		for (Registration registration : registrations) {
			if (registration.getOfferedCourse().getPrimaryKey().equals(offeredcourseKey)) {
				offeredCourseRegistrations.add(registration);
			}
		}
		return offeredCourseRegistrations;
	}
}